////
////  ViewController.m
////  Family
////
////  Created by 王声䘵 on 2018/4/16.
////  Copyright © 2018年 王声䘵. All rights reserved.
////
//

#import "ViewController.h"
#import "PST_MenuView/PST_MenuView.h"
#import <QuartzCore/QuartzCore.h>
#import "Specific_Name.h"
#import "clickName.h"
#include "MenuViewController.h"
#import "MyNavigationController.h"
#import "Singleton.h"
#import "UserViewController.h"



#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
#define PI 3.1415926
@interface ViewController ()
@property(nonatomic,strong)UIView *leftView;
@property(nonatomic,strong)UIView *rightView;
@property(nonatomic,strong)UIImageView *middleView;
@property(nonatomic,strong)UIButton *personBut;
@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //随机加载图片
    //    i = arc4random()%2;
    //
    //    NSString *name = [[NSString alloc]initWithFormat:@"%d",i];
    _leftView.frame= CGRectMake(0, 210, MAINWINDOWWIDTH/2, MAINWINDOWHEIGHT/2);
    _rightView.frame=CGRectMake(MAINWINDOWWIDTH/2, 210, MAINWINDOWWIDTH/2, MAINWINDOWHEIGHT/2);
    
    _middleView.frame=CGRectMake(30,225, MAINWINDOWWIDTH-60, MAINWINDOWHEIGHT/2-120);
    UIImageView *rightImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 25, MAINWINDOWHEIGHT/2-90)];
    UIImageView *leftImageView=[[UIImageView alloc]initWithFrame:CGRectMake(MAINWINDOWWIDTH/2-25, 0, 25, MAINWINDOWHEIGHT/2-90)];
    UIImageView *rightBackImageView=[[UIImageView alloc]initWithFrame:CGRectMake(25, 0, MAINWINDOWWIDTH/2-25, MAINWINDOWHEIGHT/2-90)];
    UIImageView *leftBackImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, MAINWINDOWWIDTH/2-25, MAINWINDOWHEIGHT/2-90)];
    [rightImageView setImage:[UIImage imageNamed:@"画轴右"]];
    [leftImageView setImage:[UIImage imageNamed:@"画轴左"]];
    [rightBackImageView setImage:[UIImage imageNamed:@"backview"]];
    [leftBackImageView setImage:[UIImage imageNamed:@"backview"]];
   

    imageView.alpha=0;
    //_leftView.backgroundColor=[UIColor redColor];
    [_rightView addSubview:rightBackImageView];
    [_rightView addSubview:rightImageView];
    [_leftView addSubview:leftBackImageView];
    [_leftView addSubview:leftImageView];
    [UIView animateWithDuration:3
                     animations:^{
                CGRect frameLeft=self.leftView.frame;
                CGRect frameRight=self.rightView.frame;
                frameLeft.origin.x-=(MAINWINDOWWIDTH/2-30);
                frameRight.origin.x+=(MAINWINDOWWIDTH/2-30);
                imageView.alpha=1;
                self.leftView.frame=frameLeft;
                self.rightView.frame=frameRight;
            }];
    

   
    
}
-(void)menuButClick{
    UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MenuViewController *Menu=[storyBoard instantiateViewControllerWithIdentifier:@"Menu"];
    [self.navigationController pushViewController:Menu animated:YES];
}
- (void)clickPic:(UITapGestureRecognizer *)tap
{
    UIImageView *clickView = (UIImageView *)tap.view;
    clickName *name = [[clickName alloc]init];
    NSString *picname = [[NSString alloc]initWithFormat:@"%d.jpeg",i];
    UIImage *clickImage = [UIImage imageNamed:picname];
    clickView.image = clickImage;
    name.image = clickView.image;
    [self.navigationController pushViewController:name animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _leftView=[[UIView alloc]init];
    _rightView=[[UIView alloc]init];
    _middleView=[[UIImageView alloc]init];
    [_middleView setImage:[UIImage imageNamed:@"画轴中"]];
    
    NSString *namebackview = @"姓氏背景";
    UIImage *namebackimage = [UIImage imageNamed:namebackview];
    namebackimageview = [[UIImageView alloc]initWithImage:namebackimage];
    namebackimageview.frame = CGRectMake(SCREEN_SIZE.width/8, SCREEN_SIZE.height/2-SCREEN_SIZE.width/8*3, SCREEN_SIZE.width/4*3, SCREEN_SIZE.width/4*3);
    
    [self.view addSubview:namebackimageview];
    
    
    
    //[self.view addSubview:imageView];
    
    
    UIImage *btn = [UIImage imageNamed:@"按钮"];
    
    menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(SCREEN_SIZE.width-69, SCREEN_SIZE.height-87.5, btn.size.width, btn.size.height);
    [menuBtn setImage:btn forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(menuButClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIImage *btnView = [UIImage imageNamed:@"按钮背景"];
    menuView = [[UIImageView alloc]initWithImage:btnView];
    menuView.frame = CGRectMake(SCREEN_SIZE.width-80, SCREEN_SIZE.height-100, btnView.size.width, btnView.size.height);
     NSString *name = @"0";
    
    UIImage *image = [UIImage imageNamed:name];
    imageView = [[UIImageView alloc]initWithImage:image];
    imageView.frame = CGRectMake(0.5*(SCREEN_SIZE.width-image.size.width), 0.5*(SCREEN_SIZE.height-image.size.height),image.size.width, image.size.height);
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickPic:)];
    [imageView addGestureRecognizer:tap];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController setNavigationBarHidden:YES];
    UIImage *back = [UIImage imageNamed:@"backview.jpeg"];
    backview = [[UIImageView alloc]initWithImage:back];
    backview.frame = [UIScreen mainScreen].bounds;
    [self.view addSubview:backview];
    
    _personBut=[[UIButton alloc]init];
    UIImage *person = [UIImage imageNamed:@"person.png"];
    [_personBut setImage:person forState:UIControlStateNormal];
    _personBut.frame = CGRectMake(SCREEN_SIZE.width-50, 50, person.size.width, person.size.height);
    [_personBut addTarget:self action:@selector(UserBut) forControlEvents:UIControlEventTouchUpInside];
//    personView = [[UIImageView alloc]initWithImage:person];
//    personView.frame = CGRectMake(SCREEN_SIZE.width-50, 50, person.size.width, person.size.height);
//    UITapGestureRecognizer *UserImageTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(UserBut)];
//    [personView addGestureRecognizer:UserImageTap];
//    personView.hidden = NO;
    //[self.view addSubview:personView];
    _personBut.hidden=NO;
    [self.view addSubview:_personBut];
    
    
    //搜索框
    UISearchBar *search = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 40,SCREEN_SIZE.width-90 , 50)];
    search.searchBarStyle = UISearchBarStyleMinimal;
    search.placeholder = @"请输入要搜索的姓氏";
    search.delegate = self;
    search.returnKeyType = UIReturnKeySearch;
    search.showsCancelButton = NO;
    
    history_Table = [[UITableView alloc]initWithFrame:CGRectMake(20, 90, SCREEN_SIZE.width-40, 300) style:UITableViewStylePlain];
    history_Table.delegate = self;
    history_Table.dataSource = self;
    history_Table.hidden = YES;
    history_Table.separatorStyle = UITableViewCellEditingStyleNone;
    history_Table.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backview.jpeg"]];
    [history_Table reloadData];
    
    delete_histiry = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    delete_histiry.frame = CGRectMake(20, 370, SCREEN_SIZE.width-40, 50);
    [delete_histiry setTitle:@"清空历史记录" forState:UIControlStateNormal];
    [delete_histiry addTarget:self action:@selector(deletehistory) forControlEvents:UIControlEventTouchUpInside];
    delete_histiry.hidden = YES;
    
    array = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]valueForKey:@"NAMES"]];
    [self.view addSubview:history_Table];
    [self.view addSubview:delete_histiry];
    [self.view addSubview:search];
    [self.view addSubview:menuView];
    [self.view addSubview:menuBtn];
    [self.view addSubview:_middleView];
    [self.view addSubview:imageView];
    [self.view addSubview:_leftView];
    [self.view addSubview:_rightView];
}
-(void)UserBut
{
    //单例类接受Username的值
    Singleton *UserName=[Singleton create];
    //判断用户是否登录
    if(UserName.UserName!=nil)
    {
        UIStoryboard *stroyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UserViewController *userView=[stroyboard instantiateViewControllerWithIdentifier:@"userView"];
        [self.navigationController pushViewController:userView animated:YES];
    }
    else{
        UIStoryboard *stroyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UserViewController *loginView=[stroyboard instantiateViewControllerWithIdentifier:@"login"];
        [self.navigationController pushViewController:loginView animated:YES];
    }
}
- (void)deletehistory
{
    [array removeAllObjects];
    [[NSUserDefaults standardUserDefaults]setValue:array forKey:@"NAMES"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [history_Table reloadData];
    UIAlertController *delete = [UIAlertController alertControllerWithTitle:@"注意" message:@"所有历史记录已清空!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *back = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [delete addAction:back];
    [self presentViewController:delete animated:YES completion:nil];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [array removeObjectAtIndex:indexPath.row];
    [[NSUserDefaults standardUserDefaults]setValue:array forKey:@"NAMES"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [history_Table reloadData];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)search
{
    _personBut.hidden = YES;
    search.frame = CGRectMake(20, 40,SCREEN_SIZE.width-40 , 50);
    [search becomeFirstResponder];
    search.showsCancelButton = YES;
    NSLog(@"%lu",array.count);
    history_Table.hidden = NO;
    delete_histiry.hidden = NO;
    _leftView.hidden=YES;
    _rightView.hidden=YES;
    _middleView.hidden=YES;
    imageView.hidden = YES;
    namebackimageview.hidden = YES;
    backview.hidden = NO;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]];
    NSString *text = [cell valueForKey:@"text"];
    
    NSMutableArray *names = [[NSMutableArray alloc]initWithObjects:@"余",@"伍",@"汪",@"元",@"卜",nil];
    int flag = 0;
    for (int i = 0; i < names.count; i++) {
        if ([text isEqualToString:names[i]]) {
            flag = 1;
        }
    }
    if(flag)
    {
        NSString *picName = [[NSString alloc]initWithFormat:@"%@.jpeg",text];
        UIImage *clickImage = [UIImage imageNamed:picName];
        clickName *name = [[clickName alloc]init];
        name.image = clickImage;
        [self.navigationController pushViewController:name animated:YES];
    }else
    {
        UIAlertController *nofind = [UIAlertController alertControllerWithTitle:@"抱歉" message:@"您要查找的姓氏已经消失或从未存在过" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *back = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [nofind addAction:back];
        [self presentViewController:nofind animated:YES completion:nil];
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"callID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"callID"];
    }
    cell.backgroundColor = [UIColor clearColor];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = array[indexPath.row];
    return cell;
}


- (void)searchBarCancelButtonClicked:(UISearchBar *)search
{
    search.frame = CGRectMake(20, 40,SCREEN_SIZE.width-100 , 50);
    [search resignFirstResponder];
    _personBut.hidden = NO;
    search.showsCancelButton = NO;
    imageView.hidden = NO;
    _leftView.hidden=NO;
    _rightView.hidden=NO;
    _middleView.hidden=NO;
    namebackimageview.hidden = NO;
    backview.hidden = NO;
    history_Table.hidden = YES;
    delete_histiry.hidden = YES;
}


- (void)searchBarSearchButtonClicked:(UISearchBar*)search
{
    [array addObject:search.text];
    [[NSUserDefaults standardUserDefaults]setValue:array forKey:@"NAMES"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [history_Table reloadData];
    NSMutableArray *names = [[NSMutableArray alloc]initWithObjects:@"余",@"伍",@"汪",@"元",@"卜",nil];
    int flag = 0;
    for (int i = 0; i < names.count; i++) {
        if ([search.text isEqualToString:names[i]]) {
            flag = 1;
        }
    }
    if(flag)
    {
        Specific_Name *name = [[Specific_Name alloc]init];
        [self.navigationController pushViewController:name animated:YES];
    }else
    {
        UIAlertController *nofind = [UIAlertController alertControllerWithTitle:@"抱歉" message:@"您要查找的姓氏已经消失或从未存在过" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *back = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [nofind addAction:back];
        [self presentViewController:nofind animated:YES completion:nil];
    }
    search.text = @"";
}
//
//-(void)searchBar:(UISearchBar *)search textDidChange:(NSString *)searchText
//{
//
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

//@implementation ViewController
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    //随机加载图片
//    i = arc4random()%2;
//    NSLog(@"%d",i);
//
//    NSString *name = [[NSString alloc]initWithFormat:@"%d.png",i];
//    UIImage *image = [UIImage imageNamed:name];
//    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
//    imageView.frame = CGRectMake(SCREEN_SIZE.width/8, SCREEN_SIZE.height/2-SCREEN_SIZE.width/8*3, SCREEN_SIZE.width/4*3, SCREEN_SIZE.width/4*3);
//    imageView.layer.masksToBounds = YES;
//    imageView.layer.cornerRadius = SCREEN_SIZE.width/8*3;
//    //图片旋转
//    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//    rotationAnimation.toValue = [NSNumber numberWithFloat: PI*2.0];
//    rotationAnimation.duration = 15;
//    rotationAnimation.cumulative = YES;
//    rotationAnimation.repeatCount = HUGE_VALF;
//    [imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
//
//    imageView.userInteractionEnabled = YES;
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickPic:)];
//    [imageView addGestureRecognizer:tap];
//
//    [self.view addSubview:imageView];
//    _top=[[UIView alloc]initWithFrame:CGRectMake(0, -140, MAINWINDOWWIDTH, 100)];//y=60
//
//    _top.backgroundColor=[UIColor cyanColor];
//    _down=[[UIView alloc]initWithFrame:CGRectMake(0, MAINWINDOWHEIGHT+120, MAINWINDOWWIDTH, MAINWINDOWHEIGHT-120)];//y=160
//    _down.backgroundColor=[UIColor greenColor];
//
//    [self.view addSubview:_down];
//
//    [self.view addSubview:_top];
//}
//
//- (void)clickPic:(UITapGestureRecognizer *)tap
//{
//    UIImageView *clickView = (UIImageView *)tap.view;
//    clickName *name = [[clickName alloc]init];
//    NSString *picname = [[NSString alloc]initWithFormat:@"%d.jpeg",i];
//    UIImage *clickImage = [UIImage imageNamed:picname];
//    clickView.image = clickImage;
//    name.image = clickView.image;
//    [self.navigationController pushViewController:name animated:YES];
//}
//
//- (void)viewDidLoad {
//    [super viewDidLoad];
//    _flag=1;
//    self.view.backgroundColor = [UIColor whiteColor];
//    [self.navigationController setNavigationBarHidden:YES];
//    UIImage *back1 = [UIImage imageNamed:@"backview.jpeg"];
//    UIImageView *backview = [[UIImageView alloc]initWithImage:back1];
//    backview.frame = [UIScreen mainScreen].bounds;
//    [self.view addSubview:backview];
//    //下拉菜单按钮
//    _downMenu=[[UIButton alloc]initWithFrame:CGRectMake(MAINWINDOWWIDTH-60, 539, 36, 34)];
//    [_downMenu setImage:[UIImage imageNamed:@"downMenu"] forState:UIControlStateNormal] ;
//    [_downMenu addTarget:self action:@selector(ClickDownMenu) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:_downMenu];
//    //搜索框
//    UISearchBar *search = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 40,SCREEN_SIZE.width-40 , 50)];
//    search.searchBarStyle = UISearchBarStyleMinimal;
//    search.placeholder = @"请输入要搜索的姓氏";
//    search.delegate = self;
//    search.returnKeyType = UIReturnKeySearch;
//
//
//    [self.view addSubview:search];
//
//    self.titleArr = @[@"消失的姓氏",@"不为人知的姓氏",@"复姓",@"人数最多的姓氏",];
//    self.imgArr = @[@"消失的姓氏",@"不为人知的姓氏",@"复姓",@"人数最多的姓氏",];
//
//
//    // Do any additional setup after loading the view, typically from a nib.
//}
//
//
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}
////下拉菜单按钮action
//-(void)ClickDownMenu
//{
//    NSLog(@"1");
//    //跳转到四个图片的分类界面
//    UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    MenuViewController *Menu=[storyBoard instantiateViewControllerWithIdentifier:@"Menu"];
//    [self.navigationController pushViewController:Menu animated:YES];
////     PST_MenuView *menuView = [[PST_MenuView alloc] initWithFrame:CGRectMake(8, 360, 120, 178) titleArr:self.titleArr imgArr:self.imgArr arrowOffset:16 rowHeight:40 layoutType:1 directionType:1 delegate:self];
//}
//
////下拉菜单cell点击事件
//-(void)didSelectRowAtIndex:(NSInteger)index title:(NSString *)title img:(NSString *)img{
//    [UIView animateWithDuration:0.5 animations:^{
//        CGRect frame=self.top.frame;
//        CGRect frameDown=self.down.frame;
//
//        frameDown.origin.y+=(MAINWINDOWHEIGHT)*(-_flag);
//        frame.origin.y+=160*_flag;
//
//
//        _flag=-1;
//
//        self.down.frame=frameDown;
//        self.top.frame=frame;
//    }];
//    if(self.navigationController.navigationBar.isHidden==YES)
//    {
//
//    }
//    else{
//        self.navigationController.navigationBar.hidden=YES;
//    }
//
//
//
//}
//-(void)Click:(UIButton *)butn
//{
//
//
//    butn.backgroundColor=[UIColor grayColor];
//
//
//}
//-(void)Back:(UIButton *)butn{
//    [UIView animateWithDuration:0.5 animations:^{
//        CGRect frame=self.top.frame;
//        CGRect frameDown=self.down.frame;
//
//        frameDown.origin.y+=(MAINWINDOWHEIGHT)*(-_flag);
//        frame.origin.y+=160*_flag;
//
//
//        _flag=1;
//
//        self.down.frame=frameDown;
//        self.top.frame=frame;
//    }];
//    if(self.navigationController.navigationBar.isHidden==YES)
//    {
//        self.navigationController.navigationBar.hidden=NO;
//    }
//    else{
//
//    }
//}
//-(void)ClickTwo:(UIButton *)butn
//{
//    butn.backgroundColor=[UIColor cyanColor];
//
//}
//- (void)searchBarTextDidBeginEditing:(UISearchBar *)search
//{
//    search.showsCancelButton = YES;
//}
//
//- (void)searchBarCancelButtonClicked:(UISearchBar *)search
//{
//    [search resignFirstResponder];
//    search.showsCancelButton = NO;
//}
//
//
//
//- (void)searchBarSearchButtonClicked:(UISearchBar*)search
//{
//    Specific_Name *name = [[Specific_Name alloc]init];
//    [self.navigationController pushViewController:name animated:YES];
//}
//
//-(void)searchBar:(UISearchBar *)search textDidChange:(NSString *)searchText
//{
//    array = [[NSMutableArray alloc]initWithObjects:searchText, nil];
//    [[NSUserDefaults standardUserDefaults]setValue:array forKey:@"NAMES"];
//    [[NSUserDefaults standardUserDefaults]synchronize];
//    NSLog(@"%lu",array.count);
//}
//
//
//@end
